//
//  main.m
//  07-MRC省市区!
//
//  Created by dllo on 16/7/12.
//  Copyright © 2016年 高雅馨. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Province.h"
#import "Area.h"
#import "city.h"
#import "TempCity.h"
int main(int argc, const char * argv[]) {
    /**
     *  使用MRC, 重写用类处理的两个省市区。加入对应内存管理。
     */
#if 0
    {
    NSString *path = @"/Users/dllo/DLS160710/Objective-C/HomeWork/07-MRC省市区!/07-MRC省市区!/Homework-05/area.txt";
    NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray *strArray = [str componentsSeparatedByString:@"\n"];
    NSMutableArray *allProArray = [NSMutableArray array];
    for (NSString *temp in strArray) {
        if (![temp hasPrefix:@""]) {
            Province *pro = [Province areaWithName:temp];
            [allProArray addObject:pro];
        } else if ([temp hasPrefix:@" "] && ![temp hasPrefix:@"    "]) {
            Province *pro = [allProArray lastObject];
            city *cit = [city areaWithName:temp];
            [pro.array addObject:cit];
        } else {
            city *cit = [[[allProArray lastObject] array] lastObject];
            Area *area1 = [Area areaWithName:temp];
            [cit.array addObject:area1];
        }
    }
    NSLog(@"%lu", allProArray.count);
    }
#endif
#if 0
    {
    NSString *filePath = @"/Users/dllo/DLS160710/Objective-C/HomeWork/07-MRC省市区!/07-MRC省市区!/Homework-05/Location1.txt";
    NSString *buffer = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    NSArray *strArray = [buffer componentsSeparatedByString:@","];
        NSMutableArray *allTempCityArr = [NSMutableArray array];
        for (NSString *cityString in strArray) {
            NSArray *city = [cityString componentsSeparatedByString:@"|"];
            TempCity *tempCity = [[TempCity alloc] initWithName:[city firstObject] number:[city lastObject]];
            [allTempCityArr addObject:tempCity];
            [tempCity release];
        }
        NSSortDescriptor *sortTempCity = [[NSSortDescriptor alloc] initWithKey:@"cityNumber" ascending:YES];
        [allTempCityArr sortUsingDescriptors:@[sortTempCity]];
        [sortTempCity release];
        NSMutableArray *finalClassArray = [NSMutableArray array];
        for (TempCity *tempCity in allTempCityArr) {
            if ([tempCity.cityNumber hasSuffix:@"0000"]) {
                Province *pro = [Province areaWithTempCity:tempCity];
                [finalClassArray addObject:pro];
            } else if ([tempCity.cityNumber hasSuffix:@"00"] && ![tempCity.cityNumber hasSuffix:@"0000"]) {
                Province *pro = [finalClassArray lastObject];
                city *cit = [city areaWithTempCity:tempCity];
                [pro.array addObject:cit];
            } else {
                Province *pro = [finalClassArray lastObject];
                city *city = [pro.array lastObject];
                Area *area = [Area areaWithTempCity:tempCity];
                [city.array addObject:area];
            }
        }
        NSLog(@"%@", finalClassArray);
    }
#endif
#if 1
    {
        NSString *temp = @"/Users/dllo/DLS160710/Objective-C/HomeWork/07-MRC省市区!/07-MRC省市区!/Homework-05/Location1.txt";
        NSString *tempString = [NSString stringWithContentsOfFile:temp encoding:NSUTF8StringEncoding error:nil];
        NSArray *cityArr = [tempString componentsSeparatedByString:@","];
        NSMutableArray *allTempCity = [NSMutableArray array];
        for (NSString *cityString in cityArr) {
            NSArray *city = [cityString componentsSeparatedByString:@"|"];
            TempCity *tempCity = [[TempCity alloc] initWithName:[city firstObject] number:[city lastObject]];
            [allTempCity addObject:tempCity];
        }
        NSSortDescriptor *sortTempCity = [[NSSortDescriptor alloc] initWithKey:@"cityNumber" ascending:YES];
        [allTempCity sortUsingDescriptors:@[sortTempCity]];
        NSLog(@"%@",allTempCity);
        NSMutableArray *finalClassArray = [NSMutableArray array];
        for (TempCity *tempCity in allTempCity) {
            if ([tempCity.cityNumber hasSuffix:@"0000"]) {
                Province *pro = [Province areaWithTempCity:(tempCity)];
                [finalClassArray addObject:pro];
            } else if ([tempCity.cityNumber hasSuffix:@"00"] && ![tempCity.cityNumber hasSuffix:@"0000"]) {
                Province *pro = [finalClassArray lastObject];
                city *cit = [city areaWithTempCity:tempCity];
                [pro.array addObject:cit];
            } else {
                Province *pro = [finalClassArray lastObject];
                city *city = [pro.array lastObject];
                  Area*zone = [Area areaWithTempCity:tempCity];
              [city.array addObject:zone];
            }
        }
        NSLog(@"%@", finalClassArray);
    }
#endif
    return 0;
}
